export interface MerchantItem {
  id: string
  name: string
  status: 'active' | 'inactive'
  createTime: string
  category: 0 | 1 // 0=普通商户，1=特约商户
  agent?: string // 所属代理商
  provider?: string // 服务商
  remark?: string
}

// 生成指定范围内的随机数
const getRandomNumber = (min: number, max: number) => {
  return Math.floor(Math.random() * (max - min + 1)) + min
}

// 生成模拟数据
export const generateMockData = (count: number = 20): MerchantItem[] => {
  const data: MerchantItem[] = []
  // 普通商户
  data.unshift({
    id: 'M2025060311001',
    name: '支付宝商户-普通商户',
    status: 'active',
    createTime: '2025/6/3 12:00:00',
    category: 0,
    agent: '',
    provider: '',
    remark: '备注内容'
  })
  // 特约商户
  data.unshift({
    id: 'M2025060311002',
    name: '微信商户-特约商户',
    status: 'inactive',
    createTime: '2025/6/3 12:00:00',
    category: 1,
    agent: '代理商A',
    provider: '服务商B',
    remark: '备注内容'
  })
  // 生成更多模拟数据
  const baseDate = 'M20250601'
  for (let i = 0; i < count; i++) {
    const randomNum = String(getRandomNumber(10000, 99999)).padStart(5, '0')
    const id = `${baseDate}${randomNum}`
    const createTime = new Date(2025, 5, 3, getRandomNumber(0, 23), getRandomNumber(0, 59)).toLocaleString()
    const isSpecial = i % 2 === 1
    data.push({
      id,
      name: isSpecial ? `特约商户${i + 1}` : `普通商户${i + 1}`,
      status: i % 2 === 0 ? 'active' : 'inactive',
      createTime,
      category: isSpecial ? 1 : 0,
      agent: isSpecial ? `代理商${i + 1}` : '',
      provider: isSpecial ? `服务商${i + 1}` : '',
      remark: isSpecial ? '特约商户' : '普通商户'
    })
  }
  return data
}

export const mockData = generateMockData()

// 增删改查
export function addMerchant(item: MerchantItem) {
  mockData.unshift(item)
}
export function updateMerchant(id: string, item: Partial<MerchantItem>) {
  const idx = mockData.findIndex(m => m.id === id)
  if (idx > -1) Object.assign(mockData[idx], item)
}
export function deleteMerchant(id: string) {
  const idx = mockData.findIndex(m => m.id === id)
  if (idx > -1) mockData.splice(idx, 1)
}
export function getMerchant(id: string): MerchantItem | undefined {
  return mockData.find(m => m.id === id)
}